home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATATYPE.SWG / 0019_Binary To Hunk - Hunking Routine.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  3KB  |  92 lines

  1. {
  2. > Oh, btw Hunking is the conversion of three binary bytes to four ascii
  3. > bytes! Thought you should know that :)
  4. > Hmmm... so that's 3*8 bits=24 bits, into 4 ascii bytes=6 significant
  5. > bits, is 2^6, =64 different ascii characters needed. I think I can
  6. > manage that...
  7.  
  8. Well.. not exactly..
  9.  
  10. I wrote the hunking routine, and your example isn't what I want, but it's
  11. close, I want to convert a binary string to an ascii string, what I have
  12. hear is what I'd like to have the opposite of, and possibly improvements
  13. on it.
  14. }
  15.  
  16. {$A+,B-,D+,E+,F-,G-,I-,L+,N-,O-,P-,Q-,R-,S+,T-,V+,X+}
  17. {$M 16384,0,655360}
  18. uses crt,dos;
  19. Const
  20.   xtranslate: array[#0..#63] of char =
  21.     'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  22.  
  23. Function Bin2Hunk(st: string): string;
  24. var
  25.   temp: string;
  26.   i,j,times: byte;
  27. begin
  28.   temp := '';
  29.   { Figures out how many times the hunking loop with need to be run }
  30.   { (it truncates to the nearest 4) }
  31.   times := (length(st) div 4)*4;
  32.   i := 0;
  33.   if times <> 0 then
  34.     repeat
  35.       temp:=temp+char(byte(st[1+i]) shr 2);
  36.       temp:=temp+char(((byte(st[1+i]) shl 4)+(byte(st[2+i]) shr 4)) and $3F);
  37.       temp:=temp+char(((byte(st[2+i]) shl 2)+(byte(st[3+i]) shr 6)) and $3F);
  38.       temp:=temp+char(byte(st[3+i]) and $3F);
  39.       inc(i,4);
  40.     until i = times;
  41.   case length(st) mod 3 of
  42.    {0:; -- do nothing if nothing is to be done! }
  43.     1: begin
  44.       temp:=temp+char(byte(st[1+i]) shr 2);
  45.       temp:=temp+char(((byte(st[1+i]) shl 4)) and $3F);
  46.     end;
  47.     2: begin
  48.       temp:=temp+char(byte(st[1+i]) shr 2);
  49.       temp:=temp+char(((byte(st[1+i]) shl 4)+(byte(st[2+i]) shr 4)) and $3F);
  50.       temp:=temp+char(((byte(st[2+i]) shl 2)) and $3F);
  51.     end;
  52.   end;
  53.   { Map it }
  54.   for j := 1 to length(temp) do temp[j] := xtranslate[temp[j]];
  55.   Bin2Hunk := temp;
  56. end;
  57.  
  58. Function Search(subchar: char; searchstuff: array of char):byte;
  59. var i: word;
  60. begin
  61.   search := 0;
  62.   for i := 1 to sizeof(searchstuff) do
  63.     if searchstuff[i] = subchar then search := i;
  64. end;
  65.  
  66. Function Hunk2Bin(st: string): string;
  67. var j,i: byte;
  68. begin
  69.   { Demap it }
  70.   for j := 1 to length(st) do st[j] := char(search(st[j],xtranslate));
  71.   hunk2bin := st;
  72. end;
  73.  
  74. var temp: string;
  75. begin
  76.   clrscr;
  77.   temp := 'Hello';
  78.   writeln('Unhunked: ',temp);
  79.  
  80.   temp := bin2hunk(temp);
  81.   writeln('Hunked: ',temp);
  82.  
  83.   temp := hunk2bin(temp);
  84.   writeln('Dehunked: ',temp);
  85. end.
  86.  
  87. {
  88. Now if you can complete the Hunk2Bin that I started to write I'd be
  89. much obliged, I thought the Bin2Hunk was OK, but then I started trying
  90. to code the hunk2bin.. and.. ack!  Note this routine uses the same
  91. radix coding as PGP without the CRC-16 :)
  92. }